package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.RedPacketUserEntivity;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.utils.TimeUtil;

import java.util.List;

/**
 * Created by Martin on 2017/1/11.
 */

public class RedPackageDetailsAdapter extends RecyclerView.Adapter<RedPackageDetailsAdapter.ViewHolder> {

    private List<RedPacketUserEntivity> data;
    private LayoutInflater inflater;
    private Context context;

    public RedPackageDetailsAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public void refresh(List<RedPacketUserEntivity> data) {
        this.data = data;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_receive_red_package_people, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        RedPacketUserEntivity redPacketUserEntivity = data.get(position);
        if (null != redPacketUserEntivity.getIsZui() &&
                redPacketUserEntivity.getIsZui().equals("1")) {
            holder.linear_zuijia.setVisibility(View.VISIBLE);
        } else {
            holder.linear_zuijia.setVisibility(View.INVISIBLE);
        }

        holder.nickName.setText(redPacketUserEntivity.getName());
        long t = Long.parseLong(redPacketUserEntivity.getCreateTime());
        String timeStr = TimeUtil.getAllTime(t);
        holder.receiveTime.setText(timeStr);
        holder.receiveMoney.setText(redPacketUserEntivity.getAmount());
        GlideUtils.loadCircleImage(context, redPacketUserEntivity.getHeadUrl(), holder.headImg);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView headImg;
        TextView nickName;
        TextView receiveTime;
        TextView receiveMoney;
        LinearLayout linear_zuijia;

        public ViewHolder(View itemView) {
            super(itemView);
            headImg = (ImageView) itemView.findViewById(R.id.head_img);
            nickName = (TextView) itemView.findViewById(R.id.nick_name);
            receiveTime = (TextView) itemView.findViewById(R.id.receive_time);
            receiveMoney = (TextView) itemView.findViewById(R.id.receive_money);
            linear_zuijia = (LinearLayout) itemView.findViewById(R.id.linear_zuijia);
        }
    }
}
